﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Web.Hosting;

namespace Enter.Common
{
   public class ImgHelper
    {
        /// <summary>
        /// 生成缩略图
        /// </summary>
        /// <param name="oldfile"></param>
        /// <param name="newfile"></param>
        /// <param name="h"></param>
        /// <param name="w"></param>
        public static bool ShowThumbnail(string oldfile, string newfile, int h, int w,System.Drawing.Imaging.ImageFormat imgType)
        {
            bool retState = false;
            try
            {

                Image img = System.Drawing.Image.FromFile(oldfile);
                Image.GetThumbnailImageAbort myCallback = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
                int oldh = img.Height;
                int oldw = img.Width;

                int newh, neww;

                double h1 = oldh * 1.0 / h;
                double w1 = oldw * 1.0 / w;

                double f = (h1 > w1) ? h1 : w1;

                if (f < 1.0)
                {
                    newh = oldh;
                    neww = oldw;
                }
                else
                {
                    newh = (int)(oldh / f);
                    neww = (int)(oldw / f);
                }

                System.Drawing.Image myThumbnail = img.GetThumbnailImage(neww, newh, myCallback, IntPtr.Zero);

                myThumbnail.Save(newfile, imgType);

                img.Dispose();
                myThumbnail.Dispose();
                retState = true;
            }
            catch(Exception ex)
            {
                retState = false;
            }
            return retState;
        }

        private static bool ThumbnailCallback()
        {
            return false;
        }

        public static bool CreateImg(string sourcePath, string resultPath, string imgType, int w, int h, out string newimgPath)
        {
            System.Drawing.Image img1;
            bool opst = false;
          string  imgFullPath = "";
          newimgPath = "";
            string localPath = "";
            string newPath = "";

            string full_source_path = sourcePath;// HostingEnvironment.MapPath(sourcePath);
            string fielname=System.DateTime.Now.ToString("yyyyMMdd");
            string folderData = string.Empty;
            string str6 = FileHelper.CreatedFolderData(resultPath, fielname, out folderData);



            newPath = resultPath+folderData;

           

         

            try
            {
                int num = new Random().Next(100, 0x270f);
             
                string _imgName = string.Format("{0}_{1}_{2}.jpg", System.DateTime.Now.ToString("HHmmss") + DateTime.Now.Millisecond.ToString(), imgType, num.ToString());

                img1 = System.Drawing.Image.FromFile(full_source_path, false);

                imgFullPath =newPath+ _imgName;
                newimgPath = folderData + _imgName;
                localPath = string.Format("{0}", imgFullPath);//HostingEnvironment.MapPath(imgFullPath)

                System.Drawing.Image hb = new System.Drawing.Bitmap(w, h);
                //创建图片对象
                System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(hb);
                //创建画板并加载空白图像
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;


                Rectangle rec1 = new Rectangle(0, 0, w, h);

                Rectangle rec2 = new Rectangle(0, 0, img1.Width, img1.Height);
                //设置保真模式为高度保真
                g.DrawImage(img1, rec1, rec2, GraphicsUnit.Pixel);
                //开始画图
                hb.Save(localPath);
               // FileHelper.IsExistFile(imgFullPath);
                g.Dispose();
                hb.Dispose();
                img1.Dispose();
                opst = true;
            }
            catch
            {
                opst = false;
                imgFullPath = "";
            }
            return opst;

        }



    }
    
}
